﻿using System;
using System.Net;

namespace Informant.Net
{
	public class CookieWebClient : WebClient
	{
		#region Properties

		/// <summary>
		/// Gets or sets the cookie container.
		/// </summary>
		/// <value>The cookie container.</value>
		public CookieContainer CookieContainer { get; set; }

		#endregion

		#region Construction

		/// <summary>
		/// Initializes a new instance of the <see cref="CookieWebClient"/> class.
		/// </summary>
		public CookieWebClient()
			: base()
		{
			CookieContainer = new CookieContainer();
		}

		#endregion

		#region Methods

		/// <summary>
		/// Returns a <see cref="T:System.Net.WebRequest"/> object for the specified resource.
		/// </summary>
		/// <param name="address">A <see cref="T:System.Uri"/> that identifies the resource to request.</param>
		/// <returns>
		/// A new <see cref="T:System.Net.WebRequest"/> object for the specified resource.
		/// </returns>
		protected override WebRequest GetWebRequest(Uri address)
		{
			WebRequest request = base.GetWebRequest(address);
			if (request is HttpWebRequest)
			{
				(request as HttpWebRequest).CookieContainer = CookieContainer;
			}
			return request;
		}
		/// <summary>
		/// Does a GET on the provided URL.
		/// </summary>
		/// <param name="url">The URL.</param>
		/// <returns></returns>
		/// <exception cref="System.Net.WebException"/>
		public String SimpleGet(String url)
		{
			return DownloadString(url);
		}
		/// <summary>
		/// Does a POST on the provided URl.
		/// </summary>
		/// <param name="url">The URL.</param>
		/// <returns></returns>
		/// <exception cref="System.Net.WebException"/>
		public String SimplePost(String url)
		{
			return UploadString(url, "POST", "");
		}

		#endregion
	}
}
